package mining;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class FindTop10
{
	public static void main(String[] args) throws IOException
	{
//		new FindTop10(new File("./datasets/adult/adult.all.modified-0.1-5-7327-RpTreeInfoGain.0.9-1.0.rule")).run();
		findAllRuleFiles(new File("./datasets/"));
	}
	
	public static void findAllRuleFiles(File dir) throws IOException {
		if (!dir.isDirectory())
			return;
		File[] files = dir.listFiles();
		for (File file : files)
		{
			if (file.isDirectory())
				findAllRuleFiles(file);
			else if (file.isFile() && file.getName().endsWith(".rule"))
			{
				new FindTop10(file).run();
			}
		}
	}
	
	private File file;
	public FindTop10(File file) {
		this.file = file;
	}
	
	public void run() throws IOException {
		List<Pair> top10 = new ArrayList<Pair>();
		System.out.println("file: " + this.file);
		
		checkFile(top10, new BufferedReader(new FileReader(this.file)));
		for (int i = 1; ; i++)
		{
			File nextFile = new File(this.file + "." + i);
			if (nextFile.exists())
			{
				System.out.println("file: " + nextFile);
				checkFile(top10, new BufferedReader(new FileReader(nextFile)));
			} else {
				System.out.println("file does not exist: " + nextFile);
				break;
			}
		}	
		
		Collections.sort(top10);
		
		BufferedWriter bw = new BufferedWriter(new FileWriter(this.file + ".top"));
		DecimalFormat decimalFormat = new DecimalFormat("#0.000");
		for (int i = 0; i < top10.size(); i++)
		{
			Pair pair = top10.get(i);
			bw.append(decimalFormat.format(pair.conf));
			bw.append(",");
			bw.append(Integer.toString(pair.sup));
			bw.append(",");
			bw.append(decimalFormat.format(pair.lift));
			bw.append(" | ");
			bw.append(pair.row);
			bw.newLine();
			
			if (i < 10)
				System.out.println(decimalFormat.format(pair.conf) + "," + 
						pair.sup + "," + 
						decimalFormat.format(pair.lift) + " | " + pair.row);
			
			if (i == 9)
			{
				bw.append("=====================================================");
				bw.newLine();
			}
		}
		
		bw.close();
		
	}
	
	private void checkFile(List<Pair> top10, BufferedReader br) throws IOException {
		while(br.ready())
		{
			String origLine = br.readLine();
			if (!origLine.startsWith("{"))
				continue;
		
			String line = origLine.substring(origLine.indexOf("}"), origLine.length());
//			System.out.println(line);
			String[] arr = line.split(",");
			double conf = Double.parseDouble(arr[5]);
			int supAB = Integer.parseInt(arr[4]);
			double lift = Double.parseDouble(arr[7]);
			
			if (top10.size() < 50)
			{
				top10.add(new Pair(conf, supAB, lift, origLine));
			} else
			{
				if (top10.get(top10.size() - 1).conf < conf)
				{
					top10.set(top10.size() - 1, new Pair(conf, supAB, lift, origLine));
					Collections.sort(top10);
//				} else if (Math.abs(top10.get(top10.size() - 1).conf - conf) < 0.0001)
//				{
//					top10.add(new Pair(conf, line));
//					Collections.sort(top10);
				}
			}
		}
	}
	
	private class Pair implements Comparable<Pair>{
		double conf;
		int sup;
		double lift;
		String row;
		
		public Pair(double conf, int sup, double lift, String row)
		{
			this.conf = conf;
			this.sup = sup;
			this.lift = lift;
			this.row = row;
		}
		
		@Override
		public int compareTo(Pair o)
		{
			double dConf = o.conf - this.conf;
			if (dConf < 0.0001)
			{
				int dSup = o.sup - this.sup;
				if (dSup == 0)
					return (int) Math.signum(o.lift - this.lift);
				return dSup;
			} else
				return (int) Math.signum(dConf);
		}
	}
	
}
